import { Component } from "react";
import '../css/login.css'
import script from "../api/userInfo";
import Export from "../api/exportApi";
export default class Login extends Component {
    constructor(props) {
        super();
        this.state = {
            username: '',
            password: '',
            authority:''
        }
    }
    getUsername = (event) => {
        this.setState({ username: event.target.value })
    }

    getPassword = (event) => {
        this.setState({ password: event.target.value })
    }
    // 管理员申请码（666） 1为管理员  2为普通用户
    getLevel=(event)=>{
        console.log(event.target.value);
        if(event.target.value === '666'){
            this.setState({authority:"1"})
        }else{
            this.setState({authority:"0"})
        }
    }
    login = async () => {
        let user = { username: this.state.username, password: this.state.password}
        let form = new FormData()
        form.append("username",user.username)
        form.append("password",user.password)
        let result= await Export.login(form)
        console.log(result.data);
        // 
        if(result.data!== 'fail'){
            alert('登录成功')
            localStorage.setItem("userId",result.data)
            window.location.href='/main'
        }else{
            alert('输入错误')
        }
    }
    register = async () => {
        let user = { username: this.state.username, password: this.state.password, authority: this.state.authority }
        let form = new FormData()
        form.append("username",user.username)
        form.append("password",user.password)
        form.append("authority",user.authority)
        form.append("money",100000)
        let {data} = await Export.register(form)
        if(data === 'fail'){
            alert('账号已存在')
        }else{
            alert('注册成功')
        }
    }
    
    render() {
        return (
            <>
                <div className="home">
                    <div className="card-login">
                        <div className="left">
                            <h1>音乐版权交易平台</h1>
                        </div>
                        <div className="right">
                            <h1>登 录 Login
                            </h1>
                            <span>账号：</span>
                            <input type="text" placeholder="请输入账号" onChange={this.getUsername}></input>
                            <br />
                            <span>密码：</span>
                            <input type="password" placeholder="请输入密码" onChange={this.getPassword}></input>
                            <br />
                            <span>申请码</span>
                            <input type="text" placeholder="请输入管理员申请码（选填）" onChange={this.getLevel}></input>
                            <br />
                            <button className="login" onClick={this.login}>登录</button>
                            <button className="login" onClick={this.register}>注册</button>
                            <br />
                        </div>
                    </div>
                </div>
            </>
        )
    }
}